package com.sanhe.rfmuscle;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.menu.app.MyApp;
import com.menu.app.activity.BaseQuickActivity;
import com.menu.app.activity.base.MenuBaseActivity;
import com.menu.app.activity.base.PortBaseActivity;
import com.menu.app.event.StartEvent;
import com.menu.app.model.Models;
import com.menu.app.util.Constant;
import com.menu.app.util.FunctionCode;
import com.menu.app.util.Pdf;
import com.menu.app.util.pipe.BasePortEventPipe;
import com.menu.app.util.port.SerialHandler;
import com.plata.base.aclr.utils.SPHelper;
import com.sanhe.ble.BleManager;
import com.sanhe.rfmuscle.activity.BoardActivity;
import com.sanhe.rfmuscle.activity.SettingMenuActivity;
import com.sanhe.rfmuscle.activity.work.CustomInfoActivity;
import com.sanhe.rfmuscle.activity.work.ModeActivity;
import com.sanhe.rfmuscle.activity.work.Pwd2Activity;
import com.sanhe.rfmuscle.activity.work.SettingActivity;
import com.sanhe.rfmuscle.activity.work.WorkActivity;
import com.sanhe.rfmuscle.adapter.BootAdapter;
import com.sanhe.rfmuscle.adapter.MyLanguageAdapter;
import com.sanhe.rfmuscle.event.Event;
import com.sanhe.rfmuscle.pipe.PortPipe;
import com.sanhe.rfmuscle.util.Const;
import com.sanhe.rfmuscle.util.MySerialHandler;
import com.sanhe.rfmuscle.view.MyPopMenu;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

public class App extends MyApp {

    static App sInstance;

    @Override
    public String getUmId() {
        return null;
    }

    @Override
    public void onCreate() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                if (activity instanceof WorkActivity) {
                    Activity tmp = getTopActivity();
                    if (tmp instanceof BaseQuickActivity) {
                        ((WorkActivity) activity).setQuick(true, ((BaseQuickActivity) tmp).getParam());
                        ((WorkActivity) activity).setFrom(tmp.getClass().getSimpleName());
                        Log.e("TAG", "onActivityCreated: ");
                    }
                } else if (activity instanceof SettingActivity) {
                    Activity tmp = getTopActivity();
                    if (tmp != null) {
                        ((SettingActivity) activity).setFrom(tmp.getClass().getSimpleName());
                    }
                } else if (activity instanceof Pwd2Activity) {
                    Activity tmp = getTopActivity();
                    if (tmp != null) {
                        ((Pwd2Activity) activity).setFrom(tmp.getClass().getSimpleName());
                    }
                }
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
        super.onCreate();
        sInstance = this;
        Constant.degree = 0;
        Constant.LANGUAGE_ADAPTER = MyLanguageAdapter.class.getName();
        Constant.BOOT_ADAPTER = BootAdapter.class.getName();
        Constant.PRODUCT = BuildConfig.PRODUCT;
        MenuBaseActivity.popMenuClass = MyPopMenu.class;
        Event.rfShowEvent.setValue(SPHelper.getInstance(this, true).getInt("rfShow"));
        Event.leftBEvent.setValue(-1);
        Event.rightBEvent.setValue(-1);
        if(Const.jump){
            Event.leftBEvent.setValue(1);
            Event.rightBEvent.setValue(1);
            Event.rfShowEvent.setValue(1);
            StartEvent startEvent = Models.Companion.get().getStartEvent();
            if (!startEvent.getValue()) {
                startEvent.postValue(true);
            }
            SPHelper.getInstance(MyApp.getApp(), true).putInt("rent", 0).commit();
        }
        Event.rentEvent.setValue(SPHelper.getInstance(this, true).getInt("rent"));
        Event.time2Event.setValue(SPHelper.getInstance(this, true).getInt("time2"));
        Event.num1Event.setValue(SPHelper.getInstance(this, true).getLong("card", 0));
        FunctionCode.Current_Temp = SPHelper.getInstance(this, true).getInt("temp", 0);
        Const.totalTimer = SPHelper.getInstance(MyApp.getApp()).getInt(Const.TOTAL_TIMER, 0);
        Const.ver = SPHelper.getInstance(this, true).getString("ver");

        try {
            if (Build.MODEL.toLowerCase(Locale.UK).contains("quad")) {
                SPHelper.getInstance(MyApp.getApp()).putString("port", "/dev/ttyS4").commit();
            }
        } catch (Throwable e) {

        }

        BleManager.getInstance();
    }

    @Override
    public void back2Main(MenuBaseActivity.JumpPrepareListener listener) {
        ignore = true;
        Iterator<Activity> it = list.iterator();
        while (it.hasNext()) {
            Activity activity = it.next();
            if (activity instanceof PortBaseActivity && !isWorkingPage()) {
                ((PortBaseActivity) activity).exit();
            }
        }
        if (listener != null) {
            listener.onPrepared();
        }
    }

    @Override
    public boolean isWorkingPage() {
        return false;
    }

    @Override
    public Class getMainClass() {
        return ModeActivity.class;
    }

    public static App getApp() {
        return sInstance;
    }

    @Override
    public Map getPageMap() {
        HashMap<String, Class> router = new HashMap<>();
        router.put("setting", SettingMenuActivity.class);
        return router;
    }

    MySerialHandler serialHandler;

    @Override
    public SerialHandler getSerialHandler(int i) {
        if (serialHandler == null) {
            serialHandler = new MySerialHandler();
        }
        return serialHandler;
    }

    @Override
    public int getRate() {
        return 19200;
    }

    @Override
    public Class getBoard() {
        return BoardActivity.class;
    }

    @Override
    public BasePortEventPipe getPortPipe() {
        return PortPipe.getInstance();
    }

    @Override
    public Class getSettingClass() {
        return SettingMenuActivity.class;
    }

    @Override
    public Class getCustomInfoClass() {
        return CustomInfoActivity.class;
    }

    @Override
    public Pdf getPdfCreator() {
        return null;
    }
}